﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class eduEditCoursesForDeptStaff : System.Web.UI.Page
{
    DataClassesDataContext SMSDC = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            //读入staffID,写出staff名称
            int m_staffID = Convert.ToInt32(this.Request.QueryString["StaffID"]);
            this.Label1.Text = (from a in SMSDC.StaffInfos
                                where a.StaffID == m_staffID
                                select a.staffName).Single().ToString().Trim();
            BindCourses();
        }
    }

    private void BindCourses()
    {
        //登陆人属于那个部门，显示即为他部门的课程，而不是全部课程
        ChbCourseList.DataSource = this.SqlDataSource1;
        ChbCourseList.DataTextField = "CourseName";
        ChbCourseList.DataValueField = "CourseID";
        ChbCourseList.DataBind();
        int m_staffID = Convert.ToInt32(this.Request.QueryString["StaffID"]);
        var h = (from a in SMSDC.StaffsInCourses
                 where a.StaffID == m_staffID
                 select a.CourseID).ToList();


        foreach (int CourseID in h)
            ChbCourseList.Items.FindByValue(CourseID.ToString()).Selected = true;
    }
   
    //更新
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        int m_staffID = Convert.ToInt32(this.Request.QueryString["StaffID"]);
        var currCourses = (SMSDC.Edu_GetCoursesForStaff(m_staffID)).ToList();
        if (currCourses.Count > 0)
            SMSDC.Edu_RemoveStaffFromCourses(m_staffID);

        foreach (ListItem item in ChbCourseList.Items)
        {
            if (item.Selected)
                SMSDC.Edu_AddStaffToCourses(m_staffID, Convert.ToInt32(item.Value));
        }
        this.RegisterStartupScript("message", "<script language='javascript' defer>alert('已经成功更新！');</script>");
    }
}
